#ifndef _GLWIDGET_H_
#define _GLWIDGET_H_

#include <QtOpenGL/qgl.h>
#include <QKeyEvent>
#include <QString>
#include <QTimer>




class  GLWidget : public QGLWidget
{
  Q_OBJECT


public:

	GLWidget(QWidget * parent);
	virtual void initializeGL();

public slots:

	void setTypeA();
	void setTypeB();
	void setTypeC();
	void setTypeD();
	void setTypeE();
	void setTypeF();
	void setTypeG();

	void setL(int);
	void setReps(int);
	void setVel(double);
	void setDx(double);
	void setDt(double);

	void setDrawLeft(bool);
	void setDrawRight(bool);
	void setDrawBoth(bool);
	void setDrawZero(bool);

	void play();
	void step();
	void setValorsInicials();

signals:
	void enableWaveSelectors(bool);
	

private slots:
	void timerFPSslot();


protected:

	virtual void paintGL( void );
	virtual void resizeGL (int width, int height);

	virtual void keyPressEvent(QKeyEvent *e);
	virtual void keyReleaseEvent(QKeyEvent *e);
	virtual void mousePressEvent( QMouseEvent *e);
	virtual void mouseReleaseEvent( QMouseEvent *);
	virtual void mouseMoveEvent(QMouseEvent *e);

	void updateFPS(float fps);


private:
	
	bool mInitialized;

	// stats
	int   frames;
	float minFPS;
	float avgFPS;
	int   avgFPSc;
	float maxFPS;
	bool  resetedFPS;

	// timers
	QTimer timer;
	QTimer timerfps;
	int time0, timef;


	enum Apartat { APARTAT_A, APARTAT_B, APARTAT_C, APARTAT_D, 
				   APARTAT_E, APARTAT_F, APARTAT_G, NUM_APARTATS };


	Apartat m_apartat;
	int		m_intervals;
	int		m_repeticions;


	std::vector<double>	m_ona;
	std::vector<double>	m_onaPrev;

	std::vector<double>	m_ona2;
	std::vector<double>	m_onaPrev2;


	float m_ratio;

	double m_dx, m_dt, m_vel, m_t;

	bool m_dobleOna;
	bool m_drawLeft;
	bool m_drawRight;
	bool m_drawBoth;
	bool m_drawZero;
	bool m_running;


	void setValorsA();
	void setValorsB();
	void setValorsC();
	void setValorsD();
	void setValorsE();
	void setValorsF();
	void setValorsG();

	void computeOna();


};



inline void GLWidget::setTypeA() {
	m_apartat = GLWidget::APARTAT_A;
	m_dobleOna = false;
	setValorsInicials();
	emit enableWaveSelectors(m_dobleOna);
}

inline void GLWidget::setTypeB() {
	m_apartat = GLWidget::APARTAT_B;
	m_dobleOna = false;
	setValorsInicials();
	emit enableWaveSelectors(m_dobleOna);
}

inline void GLWidget::setTypeC() {
	m_apartat = GLWidget::APARTAT_C;
	m_dobleOna = false;
	setValorsInicials();
	emit enableWaveSelectors(m_dobleOna);
}

inline void GLWidget::setTypeD() {
	m_apartat = GLWidget::APARTAT_D;
	m_dobleOna = true;
	setValorsInicials();
	emit enableWaveSelectors(m_dobleOna);
}

inline void GLWidget::setTypeE() {
	m_apartat = GLWidget::APARTAT_E;
	m_dobleOna = true;
	setValorsInicials();
	emit enableWaveSelectors(m_dobleOna);
}

inline void GLWidget::setTypeF() {
	m_apartat = GLWidget::APARTAT_F;
	m_dobleOna = true;
	setValorsInicials();
	emit enableWaveSelectors(m_dobleOna);
}

inline void GLWidget::setTypeG() {
	m_apartat = GLWidget::APARTAT_G;
	m_dobleOna = false;
	setValorsInicials();
	emit enableWaveSelectors(m_dobleOna);
}

inline void GLWidget::setL(int i) {
	m_intervals = i;
	setValorsInicials();
}

inline void GLWidget::setReps(int i) {
	m_repeticions = i;
}

inline void GLWidget::setVel(double d) {
	m_vel = d;
	setValorsInicials();
}

inline void GLWidget::setDx(double d) {
	m_dx = d;
	setValorsInicials();
}

inline void GLWidget::setDt(double d) {
	m_dt = d;
	setValorsInicials();
}

inline void GLWidget::setDrawLeft(bool b) {
	m_drawLeft = b;
}

inline void GLWidget::setDrawRight(bool b) {
	m_drawRight = b;
}

inline void GLWidget::setDrawBoth(bool b) {
	m_drawBoth = b;
}

inline void GLWidget::setDrawZero(bool b) {
	m_drawZero = b;
}

inline void GLWidget::play() {
	m_running = !m_running;
}

inline void GLWidget::step() {
	computeOna();
}



#endif
